package com.zmzncs.shop.module.shop.service.impl;

import com.zmzncs.shop.common.pojo.LoginUser;
import com.zmzncs.shop.common.util.ExceptionUtil;
import com.zmzncs.shop.common.util.JwtUtil;
import com.zmzncs.shop.module.shop.mapper.ShopGoodsFileMapper;
import org.springframework.beans.BeanUtils;
import com.zmzncs.shop.module.shop.pojo.shoporder.*;
import com.zmzncs.shop.module.shop.mapper.ShopOrderMapper;
import com.zmzncs.shop.module.shop.service.ShopOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.management.InvalidApplicationException;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author 高博文
 * @since 2021-05-25
 */
@Service
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder> implements ShopOrderService {

    @Autowired
    private ShopOrderMapper shopOrderMapper;
    @Autowired
    ShopGoodsFileMapper shopGoodsFileMapper;
    @Autowired
    HttpServletRequest request;

    /**
    * 分页
    */
    @Override
    public IPage<ShopOrderDTO> page(ShopOrderSearchForm form){
        IPage<ShopOrderDTO> iPage = shopOrderMapper.list(form.getPage());

        return iPage;
    }

    /**
    * 添加
    */
    @Override
    @Transactional
    public void add(ShopOrderAddForm form){
        ShopOrder shopOrder = new ShopOrder();
        BeanUtils.copyProperties(form, shopOrder);

        shopOrderMapper.insert(shopOrder);
    }

    /**
    * 删除
    */
    @Override
    @Transactional
    public void delete(Long id){
        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setId(id);

        shopOrderMapper.deleteById(shopOrder);
    }

    /**
    * 修改
    */
    @Override
    @Transactional
    public void update(ShopOrderUpdateForm form){
        ShopOrder shopOrder = new ShopOrder();
        BeanUtils.copyProperties(form, shopOrder);

        shopOrderMapper.updateById(shopOrder);
    }

    /**
    * 详情
    */
    @Override
    public ShopOrderDTO getById(Long id){
        ShopOrderDTO shopOrderDTO = shopOrderMapper.getById(id);

        return shopOrderDTO;
    }

    /**
     * 根据核销码查询商户订单详情
     */
    @Override
    public ShopOrderDTO getByWriteOffCode(String writeOffCode,Long shopId) {
        ShopOrderDTO shopOrderDTO = shopOrderMapper.getByWriteOffCode(writeOffCode);
        if (shopOrderDTO == null){
            ExceptionUtil.businessException("订单不存在");
        }
        if (!(shopId.equals(shopOrderDTO.getShopId()))) {
            ExceptionUtil.businessException("不可跨店核销订单，请核对订单商品是否是本店商品");
        }

        return shopOrderDTO;
    }

    /**
     * 首页-今日收入
     */
    @Override
    public String todayRevenue(Long shopId) {
        return shopOrderMapper.todayRevenue(shopId);
    }

    /**
     * 首页-收入详情-商品核销收入
     */
    @Override
    public String writeOffRevenue(Long shopId, Integer dateType) {
        return shopOrderMapper.writeOffRevenue(shopId,dateType);
    }

    @Override
    public String invest(Long shopId, Integer dateType) {
        return shopOrderMapper.invest(shopId,dateType);
    }

    @Override
    public List<Double> getMonthIncome(Long shopId) {

        List<Double> incomeList = new ArrayList<>(12);
        for (int i = 0; i < 12; i++) {
            Double income = shopOrderMapper.getShopOrderIncome(shopId,i + 1);
            incomeList.add(income);
        }

        return incomeList;
    }

    @Override
    public IPage<ShopOrderDTO> getWriteOffPage(ShopOrderWriteOffForm form) {
        IPage<ShopOrderDTO> iPage = shopOrderMapper.getWriteOffPage(form.getPage(),form);
        if (iPage != null && iPage.getRecords() != null && iPage.getRecords().size() > 0){
            for (ShopOrderDTO record : iPage.getRecords()) {
                List<String> fileList = shopGoodsFileMapper.getFileByGoodsId(record.getGoodsId());
                if (fileList != null && fileList.size() > 0){
                    record.setImageUrl(fileList.get(0));
                }
            }
        }
        return iPage;
    }

    @Override
    public void writeOffCode(Long orderId) {

        LoginUser loginUser = JwtUtil.getLoginUser(request);

        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setId(orderId);
        shopOrder.setIsWriteOff(1);
        shopOrder.setWriteOffTime(new Date());
        shopOrder.setWriteOffManagerId(loginUser.getManagerId());
        shopOrderMapper.updateById(shopOrder);
    }

}
